﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyFinanceManager.Objects;

namespace MyFinanceManager.PC
{
    /// <summary>
    /// Interaction logic for ucAddNewPosting.xaml
    /// </summary>
    public partial class ucAddNewPosting : UserControl
    {
        List<CatInfo> cInfos = null;
        private string filenameCategories;
        public string FilenameCategories
        {
            get { return filenameCategories; }
            set
            {
                filenameCategories = value;
                ReloadCategories();
            }
        }

        public PostingRecord Value
        {
            get
            {
                ObjectDataProvider odp = this.FindResource("PostingRecordDS") as ObjectDataProvider;
                PostingRecord pr = odp.Data as PostingRecord;
                return pr;
            }
        }

        public ucAddNewPosting()
        {
            InitializeComponent();

            foreach (var item in Database.NamesOfAccounts)
            {
                this.Account.Items.Add(item);
            }
        }


        private void Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cInfos != null)
            {
                int catId = this.Category.SelectedIndex + 1;

                this.Subcategory.Items.Clear();

                List<CatInfo> cSub = Helper.GetSubCatInfos(catId, cInfos);

                foreach (var item in cSub)
                {
                    this.Subcategory.Items.Add(item.Name);
                }

                this.Subcategory.IsEnabled = this.Subcategory.Items.Count > 0;
            }
        }

        private void ReloadCategories()
        {
            cInfos = Helper.Load(this.filenameCategories);
            List<CatInfo> cMain = Helper.GetSubCatInfos(0, cInfos);

            foreach (var item in cMain)
            {
                ComboBoxItem cbi = new ComboBoxItem { Content = item.Name, Tag = item.CatValue };
                this.Category.Items.Add(cbi);

            }
        }
    }
}
